package com.Algorithm.HUAWEI.one20211020;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: zjz
 * @Date: 2022/02/25/10:20
 * @Description:
 * @Target:
 */
public class 进制转换 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while (in.hasNext()){
            System.out.println(dealStringToNum(in.nextLine()));
        }

    }
    // 7 0111
    // 0xA  10  0xAA 170
    // 1010     1010 1010   1010 0001
    // 0 1 2 3 4 5 6 7 8 9 A B C D E F
    // 處理，對進來的字符串進行處理
    // 1010 0001
    // 0101 1000
    static int dealStringToNum(String ss){
        int sum = 0;
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 2; i < ss.length(); i++) {
            if (Character.isDigit(ss.charAt(i))){
                stringBuffer.append(getTenByTwo(ss.charAt(i)-'0'));
            }else {
                stringBuffer.append(getTenByTwo(ss.charAt(i)-'A'+10));
            }
        }

        for (int i = 0; i < stringBuffer.length(); i++) {
            sum += (int)(stringBuffer.charAt(i)-'0')*Math.pow(2,stringBuffer.length()-i-1);
        }
        return sum;
    }

    // 16轉2進制的 7 0111
    static String getTenByTwo(int n){
        StringBuffer stringBuffer = new StringBuffer();
        if (n==0){
            stringBuffer.append("0000");
        }

        while (n>0){
            stringBuffer.append(n%2);
            n/=2;
        }

        if (stringBuffer.length()==1){
            stringBuffer.append("000");
        }else if (stringBuffer.length()==2){
            stringBuffer.append("00");
        }else if (stringBuffer.length()==3){
            stringBuffer.append("0");
        }

        return stringBuffer.reverse().toString();
    }
}
